package com.castle.fortress.admin.member.setting.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.setting.entity.MemberSettingEntity;
import com.castle.fortress.admin.member.setting.dto.MemberSettingDto;
import com.castle.fortress.admin.member.setting.mapper.MemberSettingMapper;
import com.castle.fortress.admin.member.setting.service.MemberSettingService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员设置表 服务实现类
 *
 * @author 
 * @since 2025-04-15
 */
@Service
public class MemberSettingServiceImpl extends ServiceImpl<MemberSettingMapper, MemberSettingEntity> implements MemberSettingService {
    /**
    * 获取查询条件
    * @param memberSettingDto
    * @return
    */
    public QueryWrapper<MemberSettingEntity> getWrapper(MemberSettingDto memberSettingDto){
        QueryWrapper<MemberSettingEntity> wrapper= new QueryWrapper();
        if(memberSettingDto != null){
            MemberSettingEntity memberSettingEntity = ConvertUtil.transformObj(memberSettingDto,MemberSettingEntity.class);
            wrapper.like(memberSettingEntity.getId() != null,"id",memberSettingEntity.getId());
            wrapper.like(memberSettingEntity.getUpMode() != null,"up_mode",memberSettingEntity.getUpMode());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getRechargeStatus()),"recharge_status",memberSettingEntity.getRechargeStatus());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalStatus()),"withdrawal_status",memberSettingEntity.getWithdrawalStatus());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalMethod()),"withdrawal_method",memberSettingEntity.getWithdrawalMethod());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalMin()),"withdrawal_min",memberSettingEntity.getWithdrawalMin());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalServiceCharge()),"withdrawal_service_charge",memberSettingEntity.getWithdrawalServiceCharge());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalFreeStart()),"withdrawal_free_start",memberSettingEntity.getWithdrawalFreeStart());
            wrapper.like(StrUtil.isNotEmpty(memberSettingEntity.getWithdrawalFreeEnd()),"withdrawal_free_end",memberSettingEntity.getWithdrawalFreeEnd());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<MemberSettingDto> pageMemberSetting(Page<MemberSettingDto> page, MemberSettingDto memberSettingDto) {
        QueryWrapper<MemberSettingEntity> wrapper = getWrapper(memberSettingDto);
        Page<MemberSettingEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberSettingEntity> memberSettingPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberSettingDto> pageDto = new Page(memberSettingPage.getCurrent(), memberSettingPage.getSize(),memberSettingPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberSettingPage.getRecords(),MemberSettingDto.class));
        return pageDto;
    }

    @Override
    public IPage<MemberSettingDto> pageMemberSettingExtends(Page<MemberSettingDto> page, MemberSettingDto memberSettingDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        MemberSettingEntity memberSettingEntity = ConvertUtil.transformObj(memberSettingDto,MemberSettingEntity.class);
        List<MemberSettingEntity> memberSettingList=baseMapper.extendsList(pageMap,memberSettingEntity);
        Long total = baseMapper.extendsCount(memberSettingEntity);
        Page<MemberSettingDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(memberSettingList,MemberSettingDto.class));
        return pageDto;
    }
    @Override
    public MemberSettingDto getByIdExtends(Long id){
        MemberSettingEntity  memberSettingEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(memberSettingEntity,MemberSettingDto.class);
    }

    @Override
    public List<MemberSettingDto> listMemberSetting(MemberSettingDto memberSettingDto){
        QueryWrapper<MemberSettingEntity> wrapper = getWrapper(memberSettingDto);
        List<MemberSettingEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberSettingDto.class);
    }
}

